<?php
/**
 * 按HASH 生成页面
 *
 * @param  [type]
 * @return [type]
 */
function category_build_page_by_hash($hash = '', $page = 1, $page_size = 10) {
	require_once APP_PATH . '/category/model.php';
	require_once APP_PATH . '/post/model.php';
	//获取数据
	$category_data = category_model_get_by_hash($hash);
	//输出
	if ($category_data) {
		$post_data_array = post_model_get_list_by_category_id($category_data['id'], $page, $page_size);
		$category_data['post_list'] = $post_data_array;
		$category_html = category_build_page($category_data, $page);
		return $category_html;
	} else {
		return false;
	}
}
/**
 * 生成静态页面
 * @param  array
 * @return [type]
 */
function category_build_page($category_data = array(), $page = 1) {
	$category_data = get_site_data($category_data);
	$category_html = category_build_html($category_data, $page);
	if ($category_html) {
		require_once APP_PATH . '/category/model.php';
		//更新数据库
		@category_model_update_is_build_by_hash($category_data['hash'], 1);

	} else {
		return false;
	}
	return $category_html;
}
/**
 * 写入HTML文件
 * @param  string
 * @param  array
 * @return [type]
 */
function category_build_html($category_data = array(), $page = 1, $is_admin = true) {
	//文件保存路径
	$html_file_path = "";
	if ($page == 1) {
		$html_file_path = ROOT_PATH . '/' . $category_data['category_path'] . '/index.html';
	} else {
		$html_file_path = ROOT_PATH . '/' . $category_data['category_path'] . '/index_' . $page . '.html';
	}

	//自动创建build path
	$html_file_path = filter_build_path($html_file_path);
	if (!$html_file_path) {
		return false;
	}
	$template = 'category.php';
	if (!empty($category_data['template'])) {
		$template = $category_data['template'];
	}
	$page_data = $category_data;
	$html = "";
	ob_start();
	include APP_VIEW . '/' . $template; //引入模版文件
	if ($is_admin) {
		file_put_contents($html_file_path, ob_get_contents()); //生成静态文件
	} else {
		$html = ob_get_contents();
	}
	ob_clean();
	return $html;
}

function category_preview_page($category_data = array()) {
	//数据处理
	$category_data = get_site_data($category_data);
	//模板处理
	$template = 'category.php';
	if (!empty($category_data['template'])) {
		$template = $category_data['template'];
	}
	$page_data = $category_data;
	include APP_VIEW . '/' . $template; //引入模版文件
}

function category_delete_html_by_hash($category_data = array()) {
	$html_file_path = ROOT_PATH . $category_data['category_path'];
	if (file_exists($html_file_path)) {
		if (unlink($html_file_path)) {
			return true;
		} else {
			return false;
		}
	}
	return true;
}